package b2.widget.tech
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	import b2.widget.tank.*;
	import b2.widget.warehouse.*;
	
	import com.smartfoxserver.v2.requests.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class FuseTab extends TechTab
	{
		public var originalSlot2:ObjectSlot;
		public var fuseSlot:ObjectSlot;
		
		public function FuseTab()
		{
			super();
			originalSlot2 = new ObjectSlot();
			originalSlot2.x = 120;
			originalSlot2.y = 100;
//			originalSlot2.width = 60;
//			originalSlot2.height = 50;
			
			fuseSlot = new ObjectSlot();
			fuseSlot.x = 250;
			fuseSlot.y = 100;
//			fuseSlot.width = 60;
//			fuseSlot.height = 50;
			
			addChild(originalSlot);
			addChild(originalSlot2);
			addChild(fuseSlot);
			
			//箭头
			var RightArrow:Class = ResourceHelper.getInstance().getClassByName("RightArrow2");
			var rightArrow:Sprite = new RightArrow();
			rightArrow.x = 178;
			rightArrow.y = 110;
			addChild(rightArrow);
			
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			startButton = new BasicButton(ButtonBgr, "立即合成");
			startButton.x = 223;
			startButton.y = 375;
			startButton.disable = true;
			addChild(startButton);
			
			startButton.addEventListener(MouseEvent.CLICK, onStartButtonClick)
		}
		
		private function onStartButtonClick(event:MouseEvent):void
		{
			if (startButton.disable)
				return;
			if (originalSlot.basicLevel >= 4)
				return;
			var originalObjConfig:CommonObjConfig = Config.objectConfigs[originalSlot.objectIndex];
			var objBalance:String = this.generateObjBalance();
			var successRate:int = parseInt(successRateText2.text);
			var successFlag:Boolean;
			if (Math.random() < successRate / 100)
				successFlag = true;
			else
				successFlag = false;
			//坦克
			if (originalObjConfig.type == 0)
			{
				var me:PlayerInfo = PlayerInfoManager.getInstance().me;
				if (originalSlot2.tankId == me.campTankId)
					ServerHelper.getInstance().fuseTank(originalSlot2.tankId, originalSlot.tankId, originalSlot.objectIndex, 
						originalSlot.basicLevel, originalSlot.strengthenLevel, objBalance, successFlag);
				else 
				    ServerHelper.getInstance().fuseTank(originalSlot.tankId, originalSlot2.tankId, originalSlot.objectIndex, 
				    	originalSlot.basicLevel, originalSlot.strengthenLevel, objBalance, successFlag);
				ServerHelper.getInstance().addEventListener(ServerEvent.FUSE_TANK, fuseComplete);
			}
			else 
			{
				ServerHelper.getInstance().fuseObj(originalSlot.objectIndex, originalSlot.basicLevel, originalSlot.strengthenLevel,
					originalSlot.tankId, objBalance, successFlag);
				ServerHelper.getInstance().addEventListener(ServerEvent.FUSE_OBJ, fuseComplete);
			}
			removeAll();
		}
		
		private function fuseComplete(event:ServerEvent):void
		{
			var messageBox:PopMessage;
			if (event.result == "success")
			{
				messageBox = new PopMessage("合成成功！",0x00ff00);
			}
			else
			{
				messageBox = new PopMessage("合成失败！",0xff0000);
			}
			WidgetManager.getInstance().popOut([messageBox], false, true);
			Warehouse.getInstance().refresh();
		}
		
		public override function loadObject(objectIndex:int, basicLevel:int, strengthenLevel:int, tankId:int = 0):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
			//卸下原来的物品
			removeAll();
			originalSlot.loadObj(objectIndex, basicLevel, strengthenLevel, 1, 1, tankId);
			if (objectConfig.type == 0)
				me.tankPackage.deleteTank(tankId);
			else
				me.objPackage.objectList[originalSlot.comIndex]--;
			if (objectConfig.noBasicLevel)
			{
				warningTip.text = "该物品无法进行合成！";
				startButton.disable = true;
			}
			else if (basicLevel >= 4)
			{
				warningTip.text = "已到达最高品质，无法继续合成！";
				startButton.disable = true;
			}
			else if (!objectConfig.noStrengthenLevel && strengthenLevel < 4)
			{
				warningTip.text = "强化到最高等级的物品才能合成！";
				startButton.disable = true;
			}
			else
			{
				//如果是装备/物品
				if (objectConfig.type == 1 || objectConfig.type == 2)
				{
					//刷新界面
					if (me.objPackage.objectList[originalSlot.comIndex] > 0)
					{
						originalSlot2.loadObj(objectIndex, basicLevel, strengthenLevel, 1, 1);
						me.objPackage.objectList[originalSlot2.comIndex]--;
					}
				}
				//如果是坦克
				else 
				{
					for (var i:int = 0; i < me.tankPackage.tankList.length; i ++)
					{
						if (inSameLevel(me.tankPackage.tankList[i], tankId))
						{
							originalSlot2.loadObj(objectIndex, basicLevel, strengthenLevel, 1, 1,me.tankPackage.tankList[i]);
							me.tankPackage.deleteTank(me.tankPackage.tankList[i]);
							break;
						}
					}
				}
				if (!originalSlot2.hasObj)
				{
					warningTip.text = "需要两件同样的物品才能合成!";
					startButton.disable = true;
				}
				else
				{
					startButton.disable = false;
				}
				fuseSlot.loadObj(objectIndex, basicLevel + 1, 0, -1, 1);
				loadMaterial();
			}
		}
		
		private function inSameLevel(tankId1:int, tankId2:int):Boolean
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var tank1:TankInfo = PlayerInfoManager.getInstance().tanks[tankId1];
			var tank2:TankInfo = PlayerInfoManager.getInstance().tanks[tankId2];
			if (tank1.basicLevel == tank2.basicLevel && tank1.index == tank2.index && tank1.strengthenLevel == tank2.strengthenLevel)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public override function removeAll():void
		{
			super.removeAll();
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			if (originalSlot.hasObj)
			{
				var originalObjConfig:CommonObjConfig = Config.objectConfigs[originalSlot.objectIndex];
				if (originalObjConfig.type == 1 || originalObjConfig.type == 2)
				{
					me.objPackage.objectList[originalSlot.comIndex]++;
				}
				else if (originalObjConfig.type == 0)
				{
					me.tankPackage.tankList.push(originalSlot.tankId);
				}
				originalSlot.removeObj();
			}
			if (originalSlot2.hasObj)
			{
				var originalObjConfig2:CommonObjConfig = Config.objectConfigs[originalSlot2.objectIndex];
				if (originalObjConfig2.type == 1 || originalObjConfig2.type == 2)
				{
					me.objPackage.objectList[originalSlot2.comIndex]++;
				}
				else if (originalObjConfig2.type == 0)
				{
					me.tankPackage.tankList.push(originalSlot2.tankId);
				}
				originalSlot2.removeObj();
			}
			fuseSlot.removeObj();
		}
	}
}